package com.clothing.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.clothing.bean.ArticleJson;
import com.clothing.controller.BaseController;
import com.clothing.entity.Article;
import com.clothing.entity.Article.Status;
import com.clothing.entity.User;
import com.clothing.service.ArticleService;
import com.clothing.utils.AjaxMsg;
import com.clothing.utils.Utils;
import com.clothing.utils.page.Page;
import com.clothing.utils.page.Pageable;

/**
 * @ClassName: ArticleController 
 * @Description: TODO(文章) 
 * @author Charles.Tao 
 * @date 2017年9月7日 上午9:37:02 
 * @version 1.0
 */
@RequestMapping("/admin/article")
@Controller("adminArticleController")
public class ArticleController extends BaseController
{
    @Autowired
    private ArticleService articleService;
    
    /**
     * @Title: listByType 
     * @Description: TODO(文章列表) 
     * @param @param articleTypeId
     * @param @param status
     * @param @param deleted
     * @param @param pageable
     * @param @return 设定文件 
     * @return String 返回类型  
     * @throws
     */
    @RequestMapping(value = "/getList.json", method = RequestMethod.GET)
    @ResponseBody
    public String listByType(Long articleTypeId, Status status, Boolean deleted, Pageable pageable)
    {
        Page<Article> page = articleService.findPageByTypeAndStatus(articleTypeId, status, deleted, pageable);
        JSONArray ja = null;
        
        if (page.getTotal() > 0)
        {
            ja = new JSONArray();
            
            for (Article article : page.getContent())
            {
                User user = article.getCreator();
                ArticleJson json = new ArticleJson();
                json.setId(article.getId());
                json.setContent(article.getContent());
                json.setImages(article.getFullImages());
                json.setPublishDate(article.getCreateDate());
                json.setUserId(user.getId());
                json.setNickname(user.getNickname());
                json.setHeadImgUrl(user.getHeadImgUrl());
                json.setStatus(article.getStatus());
                json.setDeleted(article.getDeleted());
                ja.add(json);
            }
        }
        
        return new AjaxMsg(true, page.getTotal(), ja).toString();
    }
    
    /**
     * @Title: check 
     * @Description: TODO(审核文章) 
     * @param @param id
     * @param @param passed
     * @param @param reason
     * @param @return 设定文件 
     * @return String 返回类型  
     * @throws
     */
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @ResponseBody
    public String check(Long id, Boolean passed, String reason)
    {
        if (null == passed)
        {
            return getParameterErrorMsg();
        }

        if (passed == false)
        {
            if (Utils.isEmpty(reason))
            {
                return AjaxMsg.failed("请填写不通过原因");
            }

            if (reason.length() > 50)
            {
                return AjaxMsg.failed("原因不能超过50个字");
            }
        }

        Article article = articleService.find(id);
        
        if (null == article || article.getDeleted())
        {
            return getParameterErrorMsg();
        }

        if (Status.unchecked.equals(article.getStatus()))
        {
            if (passed)
            {
                article.setStatus(Status.pass);
            }
            else
            {
                article.setStatus(Status.notpass);
                article.setReason(reason);
            }
            
            articleService.update(article);
        }
        else if (Status.notpass.equals(article.getStatus()))
        {
            return AjaxMsg.failed("该文章已经审核不通过");
        }
        else if (Status.pass.equals(article.getStatus()))
        {
            return AjaxMsg.failed("该文章已经审核通过了");
        }

        return AjaxMsg.success("操作成功");
    }
}
